#ifndef __M_GUI_VIEW_H__
#define __M_GUI_VIEW_H__

#define MAX_LAYOUT_WIDTH 4096
#define MAX_LAYOUT_HEIGHT 4096

typedef enum LayoutSizeType
{
    TYPE_LAYOUT_SIZE_WRAP_CONTENT = -1,
    TYPE_LAYOUT_SIZE_MATH_PARENT = -2,
} LayoutSizeType;

typedef enum LayoutOrientationType
{
    TYPE_LAYOUT_ORIENTATION_FRAME = 0,  // 帧布局
    TYPE_LAYOUT_ORIENTATION_HORIZONTAL, // 水平布局
    TYPE_LAYOUT_ORIENTATION_VERTICAL,   // 垂直布局
} LayoutOrientationType;

typedef enum LayoutGravityType
{
    TYPE_LAYOUT_GRAVITY_NONE = 0x00,
    TYPE_LAYOUT_GRAVITY_LEFT = 0x01,
    TYPE_LAYOUT_GRAVITY_RIGHT = 0x02,
    TYPE_LAYOUT_GRAVITY_TOP = 0x04,
    TYPE_LAYOUT_GRAVITY_BOTTOM = 0x08,
    TYPE_LAYOUT_GRAVITY_CENTER_HORIZONTAL = 0x10,
    TYPE_LAYOUT_GRAVITY_CENTER_VERTICAL = 0x20,
    TYPE_LAYOUT_GRAVITY_CENTER = TYPE_LAYOUT_GRAVITY_CENTER_HORIZONTAL | TYPE_LAYOUT_GRAVITY_CENTER_VERTICAL,
} LayoutGravityType;

typedef enum LayoutVisibilityType
{
    TYPE_LAYOUT_VISIBILITY_VISIBLE = 0,
    TYPE_LAYOUT_VISIBILITY_INVISIBLE,
    TYPE_LAYOUT_VISIBILITY_GONE,
} LayoutVisibilityType;

typedef struct LayoutParams
{
    int layout_gravity;
    int margin_left;
    int margin_right;
    int margin_top;
    int margin_bottom;
    int layout_x;
    int layout_y;
    int layout_w;
    int layout_h;
    int available_w;
    int available_h;
} LayoutParams;

typedef struct View View;
struct View
{
    LayoutParams params;
    int dont_free;
    int is_focused;
    int visibility;
    int orientation;
    int gravity;
    int padding_left;
    int padding_right;
    int padding_top;
    int padding_bottom;
    int measured_w;
    int measured_h;
    int wrap_w;
    int wrap_h;
    uint32_t bg_color;
    void *layoutdata;
    void *viewdata;
    void *userdata;
    int (*onFocusChange)(View *view, int hasFocus);
    int (*onMeasure)(View *view);
    int (*onDraw)(View *view);
    int (*onFinish)(View *view);
};

int ViewSetLayoutGravity(View *view, int gravity);
int ViewGetLaoyoutGravity(View *view);

int ViewSetLayoutMargin(View *view, int left, int right, int top, int bottom);
int ViewGetLayoutMargin(View *view, int *left, int *right, int *top, int *bottom);

int ViewSetLayoutSize(View *view, int layout_w, int layout_h);
int ViewGetLayoutSize(View *view, int *layout_w, int *layout_h);

int ViewSetLayoutAvailableSize(View *view, int available_w, int available_h);
int ViewGetLayoutAvailableSize(View *view, int *available_w, int *available_h);

int ViewSetLayoutPosition(View *view, int layout_x, int layout_y);
int ViewGetLayoutPosition(View *view, int *layout_x, int *layout_y);

int ViewSetVisibility(View *view, int visibility);
int ViewGetVisibility(View *view);

int ViewSetOrientation(View *view, int orientation);
int ViewGetOrientation(View *view);

int ViewSetGravity(View *view, int gravity);
int ViewGetGravity(View *view);

int ViewSetPadding(View *view, int left, int right, int top, int bottom);
int ViewGetPadding(View *view, int *left, int *right, int *top, int *bottom);

int ViewSetAutoFree(View *view, int auto_free);
int ViewIsAutoFree(View *view);

int ViewSetFocus(View *view, int focused);
int ViewIsFocused(View *view);

int ViewSetBgColor(View *view, uint32_t color);
uint32_t ViewGetBgColor(View *view);

int ViewSetUserData(View *view, void *data);
void *ViewGetUserData(View *view);

// 调用ViewMeasure后才能获取正确的值
int ViewGetMeasuredSize(View *view, int *measured_w, int *measured_h);
int ViewGetWrapSize(View *view, int *wrap_w, int *wrap_h);
int ViewGetOccupySize(View *view, int *occupy_w, int *occupy_h);
int ViewGetMeasuredContentSize(View *view, int *measured_content_w, int *measured_content_h);
int ViewGetWrapContentSize(View *view, int *wrap_content_w, int *wrap_content_h);

int ViewMeasure(View *view);
int ViewDraw(View *view);
int ViewFinish(View *view);

#endif